const {
    $Message
  } = require('/component/iView/base/index');
  
  App({
    globalData: {
      // baseAPI: "https://yxyy.yxmsh.com",
      baseAPI: "https://www.yrtzyg.com",
      pageSize: 7
    },
  
  
  
    onLaunch: function() {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        console.log(res.hasUpdate)
      })

      updateManager.onUpdateReady(function () {
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success: function (res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate()
            }
          }
        })
      })
      let _this = this

      let token = wx.getStorageSync('token')

      console.log("token是"+token)
      if (null == token || token == '') {
        if(wx.qy){
          wx.qy.login({
            success(wxres) {
              if (wxres.code) {
                _this.formGet('/inApi/qywx/student/auth/appletLogin', {
                  "code": wxres.code
                }).then(res => {
                  console.log(res)
                  if (res.code == 1) {
                    wx.setStorageSync('token', res.response.token)
                    wx.reLaunch({
                      url: '/pages/index/index',
                    });
                  } 
                  // else if (res.code == 401) {
                  //   wx.reLaunch({
                  //     url: '/pages/403/index',
                  //   });
                  // } 
                  else {
                    _this.message(res.message, '您无该系统的权限，请联系系统管理员')
  
                        wx.reLaunch({
                      url: '/pages/403/index',
                    });
                  }
                }).catch(e => {
                  _this.message(e, 'error')
                })  
              } else {
                _this.message(res.errMsg, 'error')
              }
            }
          })
        }else{
          wx.login({
            success(wxres) {
              if (wxres.code) {
                _this.formPost('/api/wx/student/auth/checkBind', {
                  "code": wxres.code
                }).then(res => {
                  if (res.code == 1) {
                    wx.setStorageSync('token', res.response)
                    wx.reLaunch({
                      url: '/pages/index/index',
                    });
                  } else if (res.code == 2) {
                    wx.login({
                      success(wxres) {
                        if (wxres.code) {
                          const params = {
                            userName:'郭星星',
                            password:'123456',
                            code:wxres.code
                          }
                          _this.formPost('/api/wx/student/auth/bind', params)
                            .then(res => {
                              if (res.code == 1) {
                                wx.setStorageSync('token', res.response)
                                wx.reLaunch({
                                  url: '/pages/index/index',
                                });
                              } else {
                                _this.message(res.message, 'error')
                              }
                            }).catch(e => {
                              _this.message(e, 'error')
                            })
                        } else {
                          _this.message(res.errMsg, 'error')
                        }
                      }
                    })
                  } else {
                    _this.message(res.message, 'error')
                  }
                }).catch(e => {
                  _this.message(e, 'error')
                })
              } else {
                _this.message(res.errMsg, 'error')
              }
            }
          })
        }
        
      }
    },
    message: function(content, type) {
      $Message({
        content: content,
        type: type
      });
    },
    formPost: function(url, data) {
      let token = wx.getStorageSync('token')

      console.log("token是"+token)

      let _this = this
      return new Promise(function(resolve, reject) {
        wx.showNavigationBarLoading();
        wx.request({
          url: _this.globalData.baseAPI + url,
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'token': wx.getStorageSync('token')
          },
          method: 'POST',
          data,
          success(res) {
            
            if (res.statusCode !== 200 || typeof res.data !== 'object') {
              reject('网络出错')
              return false;
            }
  
            if (res.data.code === 400) {
              let token = res.data.response
              wx.setStorageSync('token', token)
              wx.request({
                url: _this.globalData.baseAPI + url,
                header: {
                  'content-type': 'application/x-www-form-urlencoded',
                  'token': wx.getStorageSync('token')
                },
                method: 'POST',
                data,
                success(result) {
                  resolve(result.data);
                  return true;
                }
              })
            } else if (res.data.code === 405) {
              wx.setStorageSync('token', '')
              wx.reLaunch({
                      url: '/pages/index/index',
                    });
              return false;
            } else if (res.data.code === 500) {
              reject(res.data.message)
              return false;
            } else if (res.data.code === 501) {
              reject(res.data.message)
              return false;
            } else {
              resolve(res.data);
              return true;
            }
          },
          fail(res) {
            reject(res.errMsg)
            return false;
          },
          complete(res) {
            wx.hideNavigationBarLoading();
          }
        })
      })
    },
   formPost: function(url, data) {
      let _this = this
      return new Promise(function(resolve, reject) {
        wx.showNavigationBarLoading();
        wx.request({
          url: _this.globalData.baseAPI + url,
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'token': wx.getStorageSync('token')
          },
          method: 'POST',
          data,
          success(res) {
            
            if (res.statusCode !== 200 || typeof res.data !== 'object') {
              reject('网络出错')
              return false;
            }
  
            if (res.data.code === 400) {
              let token = res.data.response
              wx.setStorageSync('token', token)
              wx.request({
                url: _this.globalData.baseAPI + url,
                header: {
                  'content-type': 'application/x-www-form-urlencoded',
                  'token': wx.getStorageSync('token')
                },
                method: 'POST',
                data,
                success(result) {
                  resolve(result.data);
                  return true;
                }
              })
            } else if (res.data.code === 405) {
              wx.setStorageSync('token', '')
              wx.reLaunch({
                      url: '/pages/index/index',
                    });
              return false;
            } else if (res.data.code === 500) {
              reject(res.data.message)
              return false;
            } else if (res.data.code === 501) {
              reject(res.data.message)
              return false;
            } else {
              resolve(res.data);
              return true;
            }
          },
          fail(res) {
            reject(res.errMsg)
            return false;
          },
          complete(res) {
            wx.hideNavigationBarLoading();
          }
        })
      })
    },
    formPostToBody: function(url, data) {
      let _this = this
      return new Promise(function(resolve, reject) {
        wx.showNavigationBarLoading();
        wx.request({
          url: _this.globalData.baseAPI + url,
          header: {
            'content-type': 'application/json',
            'token': wx.getStorageSync('token')
          },
          method: 'POST',
          data,
          success(res) {
            
            if (res.statusCode !== 200 || typeof res.data !== 'object') {
              reject('网络出错')
              return false;
            }
  
            if (res.data.code === 400) {
              let token = res.data.response
              wx.setStorageSync('token', token)
              wx.request({
                url: _this.globalData.baseAPI + url,
                header: {
                  'content-type': 'application/json',
                  'token': wx.getStorageSync('token')
                },
                method: 'POST',
                data,
                success(result) {
                  resolve(result.data);
                  return true;
                }
              })
            } else if (res.data.code === 405) {
              wx.setStorageSync('token', '')
              wx.reLaunch({
                      url: '/pages/index/index',
                    });
              return false;
            } else if (res.data.code === 500) {
              reject(res.data.message)
              return false;
            } else if (res.data.code === 501) {
              reject(res.data.message)
              return false;
            } else {
              resolve(res.data);
              return true;
            }
          },
          fail(res) {
            reject(res.errMsg)
            return false;
          },
          complete(res) {
            wx.hideNavigationBarLoading();
          }
        })
      })
    },
    formGetToBody: function(url, data) {
        let _this = this
        return new Promise(function(resolve, reject) {
          wx.showNavigationBarLoading();
          wx.request({
            url: _this.globalData.baseAPI + url,
            header: {
              'content-type': 'application/json',
              'token': wx.getStorageSync('token')
            },
            method: 'GET',
            data,
            success(res) {
              
              if (res.statusCode !== 200 || typeof res.data !== 'object') {
                reject('网络出错')
                return false;
              }
    
              if (res.data.code === 400) {
                let token = res.data.response
                wx.setStorageSync('token', token)
                wx.request({
                  url: _this.globalData.baseAPI + url,
                  header: {
                    'content-type': 'application/json',
                    'token': wx.getStorageSync('token')
                  },
                  method: 'POST',
                  data,
                  success(result) {
                    resolve(result.data);
                    return true;
                  }
                })
              } else if (res.data.code === 405) {
                wx.setStorageSync('token', '')
                wx.reLaunch({
                        url: '/pages/index/index',
                      });
                return false;
              } else if (res.data.code === 500) {
                reject(res.data.message)
                return false;
              } else if (res.data.code === 501) {
                reject(res.data.message)
                return false;
              } else {
                resolve(res.data);
                return true;
              }
            },
            fail(res) {
              reject(res.errMsg)
              return false;
            },
            complete(res) {
              wx.hideNavigationBarLoading();
            }
          })
        })
      },
    
    getToken:function(){
  
      console.log("获取token");
      wx.qy.login({
          success(wxres) {
            if (wxres.code) {
  
              _this.formGet('/inApi/qywx/student/auth/appletLogin', {
                "code": wxres.code
              }).then(res => {
                console.log(res)
                if (res.code == 1) {
                  wx.setStorageSync('token', res.response.token)
                  wx.reLaunch({
                    url: '/pages/index/index',
                  });
                }
                else {
                  _this.message(res.message, '您无该系统的权限，请联系系统管理员')
  
                      wx.reLaunch({
                    url: '/pages/403/index',
                  });
                }
              }).catch(e => {
                _this.message(e, 'error')
              })  
            } else {
              _this.message(res.errMsg, 'error')
            }
          }
        })
    },
    formGet:function(url,data){
      let _this = this
      return new Promise(function(resolve, reject) {
        wx.showNavigationBarLoading();
        wx.request({
          url: _this.globalData.baseAPI + url,
          header: {
            'content-type': 'application/json'
          },
          method:"GET",
          data,
          success(res) {
            
              console.log("你好"+res)
            
            if (res.statusCode !== 200 || typeof res.data !== 'object') {
              reject('网络出错')
              return false;
            }
  
            if (res.data.code === 405) {
              wx.setStorageSync('token', '')
              wx.reLaunch({
                      url: '/pages/index/index',
                    });
              return false;
            } else if (res.data.code === 500) {
              reject(res.data.message)
              return false;
            } else if (res.data.code === 501) {
              reject(res.data.message)
              return false;
            } else {
              resolve(res.data);
              return true;
            }
          },
          fail(res) {
            console.log('fail')
            reject(res.errMsg)
            return false;
          },
          complete(res) {
            wx.hideNavigationBarLoading();
          }
        })
      })
    }
  })